package org.example.xxljob;

import cn.hutool.core.util.StrUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;

import org.example.config.AlipayConts;
import org.example.domain.Zero;
import org.example.servide.ZeroService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.mail.FetchProfile;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * XxlJob开发示例（Bean模式）
 *
 * 开发步骤：
 * 1、在Spring Bean实例中，开发Job方法，方式格式要求为 "public ReturnT<String> execute(String param)"
 * 2、为Job方法添加注解 "@XxlJob(value="自定义jobhandler名称", init = "JobHandler初始化方法", destroy = "JobHandler销毁方法")"，注解value值对应的是调度中心新建任务的JobHandler属性的值。
 * 3、执行日志：需要通过 "XxlJobLogger.log" 打印执行日志；
 *
 * @author xuxueli 2019-12-11 21:52:51
 */
@Component
public class SampleXxlJob {


    @Autowired
    ZeroService zeroService;



    /**
     * 1、简单任务示例（Bean模式）
     */
    @XxlJob("demoJobHandler")
    public ReturnT<String> demoJobHandler(String param) throws Exception {
        System.out.println("XXL-JOB, Hello World.");
        Transfer_Order();
        return ReturnT.SUCCESS;
    }


    private void Transfer_Order() throws AlipayApiException {
        List<Zero> lookstatus = zeroService.lookstatus();
        for (Zero zero : lookstatus) {
            AlipayClient alipayClient = AlipayConts.ALIPAY_CLIENT;

            // 构造请求参数以调用接口
            AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
            AlipayTradeQueryModel model = new AlipayTradeQueryModel();

            // 设置订单支付时传入的商户订单号
            model.setOutTradeNo(zero.getOutTradeNo());

            // 设置查询选项
            List<String> queryOptions = new ArrayList<String>();
            queryOptions.add("trade_settle_info");
            model.setQueryOptions(queryOptions);

            // 设置支付宝交易号
//            model.setTradeNo("2014112611001004680 073956707");

            request.setBizModel(model);
            AlipayTradeQueryResponse response = alipayClient.execute(request);
            System.out.println(response.getBody());

            if (response.isSuccess() && StrUtil.equalsIgnoreCase(response.getTradeStatus(),AlipayConts.TRADE_SUCCESS)) {
                System.out.println("调单调用成功");
                String outTradeNo = response.getOutTradeNo();
                String tradeNo = response.getTradeNo();
                String totalAmount = response.getTotalAmount();
                zeroService.docallback(outTradeNo,tradeNo,new BigDecimal(totalAmount));
            }


        }
    }


}
